<?php
  require_once('tpl/header.php');
?>

   <?php 
//用session判定管理员是否登录
session_start();
if (!isset($_SESSION["adminname"])) {//如果session中没有管理员登录名
	header("Location:index.php?msg=您没有权限，请登录后访问");
}


		connect();


	//当前页号
	if (!isset($_GET['page'])) {
		$page=1;
	}else{
		$page=$_GET["page"];
	}
	$sql="select * from videos";
	if (isset($_GET["key"])) {
		$key=$_GET["key"];
		$sql=$sql." where videoname like'%{$key}%'";

	}

	// var_dump($sql);
	// var_dump($_GET["key"]);
	$result=mysql_query($sql);//结果集
	$totalrows=mysql_num_rows($result);//统计结果集中条数
	$rowsperpage=5;//每页显示
	if ($totalrows%$rowsperpage==0) {
		$totalpages=$totalrows/$rowsperpage;//共分了多少页
			}else{
				$totalpages=ceil($totalrows/$rowsperpage);
			}
	$start=($page-1)*$rowsperpage;//每页起始记录编号

	$sql1=$sql." limit $start,$rowsperpage";
	$result1=mysql_query($sql1);//每页的结果集
 ?>
<body>
	<form class="form-inline" action="" method="get">
		<div class="form-group">
			<label for="exampleInputName2">视频名称:</label>
			<input type="text" class="form-control" id="exampleInputName2" placeholder="视频名称" name="key">
		</div>
		<input type="submit" value="搜索" class="btn btn-default">
	</form>
	<?php 
	if($totalrows==0){
		die("<br>没有搜索到用户");
	}
	 ?>
	
	<table class="table table-hover">
		<caption>视频（共 <?php echo $totalrows; ?>个）</caption>
		<tr>
			<th>视频编号</th>
			<th>视频名称</th>
			<th>视频类别</th>
			<th>添加时间</th>
			<th>视频海报</th>
			<th>操作</th>
		</tr>
		
		<?php 
		while ($row1=mysql_fetch_assoc($result1)) 
		{
			?>
			<tr>
			<td><?php echo $row1["vid"]; ?></td>
			<td><?php echo $row1["videoname"]; ?></td>
			<td><?php 
				$tid=$row1["tid"];
				$sql_vtype="select * from videotype where tid=$tid";
				$result2=mysql_query($sql_vtype);
				$row_vtype=mysql_fetch_assoc($result2);
				echo $row_vtype["typename"];

			 ?></td>
			<td><?php echo $row1["uploaddate"]; ?></td>
			<td><img src="../posters/<?php echo $row1["pic"]; ?>" width="100px"  height="auto" title="<?php echo $row1['intro'] ?>"></td>
			<td><a href="videoEdit.php?vid=<?php echo$row1['vid']; ?>">修改 </a>|<a href="doVideoDelete.php?vid=<?php echo$row1['vid']; ?>" onclick="return confirm('你真的要删除吗？')"> 删除</a></td>
			</tr>
		<?php
	
		}//while的结束
	// }//判断是否查到记录的else的结束

		 ?>
		
	</table>

	<h3>
		<?php 
			echo "共有条".$totalrows."记录"."&nbsp;&nbsp;"."共分了".$totalpages."页"."&nbsp;&nbsp;";
		//首页和上一页的实现
			if ($page==1) {
				echo "首页"."&nbsp;&nbsp;";
			}else{
				echo "<a href=?page=1>首页</a>"."&nbsp;&nbsp;";
			}

			if ($page==1) {
				echo "上一页"."&nbsp;&nbsp;";
			}else{
				echo "<a href=?page=".($page-1).">上一页</a>"."&nbsp;&nbsp;";
			}
		//第i页的超链接
			for ($i=1; $i<=$totalpages ; $i++) { 
				echo "<a href=?page=$i>第{$i}页</a>"."&nbsp;&nbsp;";
			}
		//尾页和下一页的实现
			if ($page==$totalpages) {
				echo "下一页"."&nbsp;&nbsp;";
			}else{
				echo "<a href=?page=".($page+1).">下一页</a>"."&nbsp;&nbsp;";
			}
			if ($page==$totalpages) {
				echo "尾页"."&nbsp;&nbsp;";
			}else{
				echo "<a href=?page=$totalpages>尾页</a>"."&nbsp;&nbsp;";
			}
		 ?>
	</h3>
</body>

<?php
  require_once('tpl/footer.php');
?>